<?php
class Sys extends  Controller
{
    function Sys()
    {
        parent::Controller();
      
		header("Content-Type: text/html; charset=utf-8");
		
		$this->userdata = checksession($this->session,"systemdata");
		
    }
    function set()
    {
    	//swritefile("./data/system.inc.php",'ddd');
    	
    	$num = self::checkall($this->userdata["companyid"]);
    	
    	
    	
    	$_cache_system = array();
    	
    	
    	
    	if(file_exists("./data/system.cache.php"))
    	{
    		require './data/system.cache.php';
    		
    	}
    	$this->mytp->assign("username",$this->session->userdata('username'));
    	$this->mytp->assign("full_page",1);
    	$this->mytp->assign("cache",$_cache_system);
    	$this->mytp->assign("num",$num);
    	$this->mytp->assign("page_title","系统设置");
    	$this->mytp->assign("nowsysdate",date("Y-m-d H:i:s"));
    	$this->mytp->tp("sys_set");
    	
    }
    function saveset()
    {
    	$endtime = $_POST["times"];
    	
    	$notice = $_POST["notice"];
    	if(isset($endtime) && isset($notice))
    	{
    		$_system = array(
    		'endtime'=>$endtime,
    		'notice'=>$notice
    		);
    		swritefile("./data/system.cache.php","<?php \r\n\$_cache_system=".var_export($_system,1)."\r\n?>");
    		
    	}
    	s("设置成功",baseurl."sys/set/");
    }
    function pass()
    {
    	
    	$this->mytp->assign("username",$this->session->userdata('username'));
    	$this->mytp->assign("full_page",1);
    	$this->mytp->assign("page_title","修改密码");
    	$this->mytp->tp("sys_pass");
		 
    }
    function savepass()
    {
    	$pass = $_REQUEST["pass"];
    	$repass = $_REQUEST["repass"];
    	if(empty($pass) && $pass!=$repass)
    	{
    		e("密码不能为空或两次密码不一致");
    	}else
    	{
    		$this->db->query("update ".table("systemuser")." set password='".md5($pass)."' where uid=".$this->session->userdata('userid'));
    		s("密码修改成功",baseurl."sys/pass");
    	}
    }
    
	function checkall($companyid=1)#计算总金额
    {
    	
    	
		
		if($companyid==2)
		{
			$query_sum = $this->db->query("select sum(balance) as price from ".table("users")." where companyid={$companyid}");
			
		    $pay_price = $query_sum->row_array();
    	
		 	$info = $pay_price["price"];
		}else
		{
			$query_sum = $this->db->query("select foodprice,foodnum from ".table("orders")." where companyid={$companyid} and isdo=1");
			$person_expense = $query_sum->result_array();
			
			$foodprice = 0;
			foreach ($person_expense as $f)
			{
				$foodprice+=$f["foodprice"]*$f["foodnum"];
			}
			
			$query_price = $this->db->query("select sum(price) as price from ".table("topup")." where companyid={$companyid}");
		 	$pay_price = $query_price->row_array();
		 
    	
		 	$info = ($pay_price['price']-$foodprice)+1464;#加上之前余额1355
			
		}
		
		 
		 return $info;
		
    }
    
}    
?>